package com.tansun.easycare.rule.common.service.impl;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tansun.easycare.rule.codeblock.service.RuleCodeblockService;
import com.tansun.easycare.rule.common.BomCreator;
import com.tansun.easycare.rule.common.Constant;
import com.tansun.easycare.rule.common.DataCache;
import com.tansun.easycare.rule.common.enums.ContentTypeEnum;
import com.tansun.easycare.rule.common.jfun.JFun;
import com.tansun.easycare.rule.common.model.Bom;
import com.tansun.easycare.rule.common.model.BomField;
import com.tansun.easycare.rule.common.model.Domain;
import com.tansun.easycare.rule.common.rulebasebean.Fact;
import com.tansun.easycare.rule.common.service.DataBomService;
import com.tansun.easycare.rule.common.service.RuleDrlParseService;
import com.tansun.easycare.rule.datamodel.dao.RuleDataparamsDao;
import com.tansun.easycare.rule.datamodel.entity.RuleDataparams;
import com.tansun.easycare.rule.datamodel.entity.RuleDataset;
import com.tansun.easycare.rule.datamodel.service.RuleDataparamsService;
import com.tansun.easycare.rule.referencedata.entity.RuleReferenceData;
import com.tansun.easycare.rule.referencedata.service.RuleReferenceDataService;
import com.tansun.easycare.rule.rulegroup.service.RuleParseDrl;
import com.tansun.easycare.rule.rulemange.entity.RuleManage;
import com.tansun.easycare.rule.ruletest.model.RuleItem;
import com.tansun.easycare.rule.ruletest.model.TestDataModeVO;
import com.tansun.easycare.rule.ruletest.model.TestDatasetVO;

@Service
public class RuleDrlParseServiceImpl implements RuleDrlParseService {

    @Autowired
    private DataBomService dataBomService;

    @Autowired
    private RuleDataparamsDao ruleDataparamsDao;
    
    @Autowired
    private RuleReferenceDataService ruleReferenceDataService;
    
    @Autowired
    private RuleCodeblockService ruleCodeblockService;
    
	@Autowired
	private  RuleDataparamsService  ruleDataparamsService;

    
    @Override
    public TestDataModeVO getDataMode(String drlContent, String projectCode) throws Exception {
        String drl = drlContent;

        TestDataModeVO testDataModeVO = new TestDataModeVO();
        // drl数据
        Set<String> drlModelSet = new HashSet<String>();
        Set<String> drlModelThenSet = new HashSet<String>();

        getDrlModelSet(projectCode,drl,drlModelSet,drlModelThenSet);
        // 读取模型数据,进行匹配

        Map<String, RuleDataset> map = dataBomService.findRuleDataset(projectCode);
        Collection<RuleDataset> list = map.values();
        List<TestDatasetVO> inputList = new ArrayList<TestDatasetVO>();
        List<TestDatasetVO> thenList = new ArrayList<TestDatasetVO>();
        Map<String, List<String>> getModelMap = new HashMap<String, List<String>>();
        Map<String, List<String>> getThenModelMap = new HashMap<String, List<String>>();

        for (String m : drlModelSet) {
            if (m.equals(""))
                continue;
            if (getModelMap.get(m.split("\\.")[0]) != null) {
                getModelMap.get(m.split("\\.")[0]).add(m.split("\\.")[1]);
            } else if (m.split("\\.").length == 2) {
                List<String> getModelAttrList = new ArrayList<String>();
                getModelAttrList.add(m.split("\\.")[1]);
                getModelMap.put(m.split("\\.")[0], getModelAttrList);

            }
        }
        // then数据
        for (String m : drlModelThenSet) {
            if (m.equals(""))
                continue;
            if (getThenModelMap.get(m.split("\\.")[0]) != null) {
                getThenModelMap.get(m.split("\\.")[0]).add(m.split("\\.")[1]);
            } else {
                List<String> getModelAttrList = new ArrayList<String>();
                getModelAttrList.add(m.split("\\.")[1]);
                getThenModelMap.put(m.split("\\.")[0], getModelAttrList);
            }
        }
        for (RuleDataset dm : list) {
            for (Entry<String, List<String>> tds : getThenModelMap.entrySet()) {
                if (dm.getName().equals(tds.getKey())) {
                    List<RuleItem> attrList = new ArrayList<RuleItem>();
                    RuleItem testItem = null;
                    for (String attr : tds.getValue()) {
                        //attrList.add(as.getAttributeByName(tds.getKey(), attr, path));
                        testItem = new RuleItem();
                        this.getRuleTestItem(testItem, projectCode, dm.getName(), attr);
                        testItem.setClassName(dm.getName());
                        testItem.setClassNamecn(dm.getChzn());
                        attrList.add(testItem);
                    }
                    TestDatasetVO datasetVO = new TestDatasetVO();
                    datasetVO.setChzn(dm.getChzn());
                    datasetVO.setName(dm.getName());
                    datasetVO.setAttrList(attrList);
                    thenList.add(datasetVO);

                }
            }
            for (Entry<String, List<String>> ds : getModelMap.entrySet()) {
                if (dm.getName().equals(ds.getKey())) {
                    List<RuleItem> attrList = new ArrayList<RuleItem>();
                    RuleItem testItem = null;
                    for (String attr : ds.getValue()) {
                        //attrList.add(as.getAttributeByName(ds.getKey(), attr, path));
                        testItem = new RuleItem();
                        this.getRuleTestItem(testItem, projectCode, dm.getName(), attr);
                        testItem.setClassName(dm.getName());
                        testItem.setClassNamecn(dm.getChzn());
                        attrList.add(testItem);
                    }
                    TestDatasetVO datasetVO = new TestDatasetVO();
                    datasetVO.setChzn(dm.getChzn());
                    datasetVO.setName(dm.getName());
                    datasetVO.setAttrList(attrList);
                    inputList.add(datasetVO);
                }
            }
        }
        testDataModeVO.setInputDataModel(inputList);
        testDataModeVO.setThenDataModel(thenList);
        return testDataModeVO;
    }
    /**
     * 正则匹配，解析结果存放在集合中
     * @param projectCode
     * @param drl
     * @param drlModelSet
     * @param drlModelThenSet
     * @throws Exception
     */
    public void getDrlModelSet(String projectCode,String drl,Set<String> drlModelSet,Set<String> drlModelThenSet) throws Exception {
        BufferedReader drlStr = null;
        try {

            ByteArrayInputStream bis = new ByteArrayInputStream(drl.getBytes(Constant.DEFAULT_CHARSET));
            drlStr = new BufferedReader(new InputStreamReader(bis));
            // 捡查是否数据模型
            while ((drl = drlStr.readLine()) != null) {
                drlModelSet.addAll(matcherRuleFile(drl));
                drlModelSet.addAll(matcherMapRuleFile(drl));
                drlModelSet.addAll(matcherMapRuleFileDataMap(drl));
                drlModelSet.addAll(matcherOtherRuleFile5(drl));
                drlModelSet.addAll(matcherOtherRuleFile2(drl));
                drlModelSet.addAll(matcherRuleFunctionFile(drl));
                drlModelThenSet.addAll(matcherOtherRuleFile7(drl));
                drlModelThenSet.addAll(matcherOtherRuleFile(drl));
				drlModelThenSet.addAll(matcherOtherRuleFile6(drl));
                drlModelThenSet.addAll(matcherInsertValueRuleFile(drl));
                Set<String> mathFormuls = matcherOtherRuleFile3(drl);
                for (String item : mathFormuls) {
                    if (item.indexOf("{") > -1) {
                        BomField field = this.getAttrById(projectCode, item.substring(1, item.length() - 1));
                        drlModelSet.add(field.getClassName() + "." + field.getName());
                    } else {
                        drlModelThenSet.add(item);
                    }
                }
            }
            // 过滤input，输入输出同时有值时，去除输入保留输出
//            for (String m : drlModelThenSet) {
//                if (m.equals(""))
//                    continue;
//                for (String mm : drlModelSet) {
//                    if (mm.equals(""))
//                        continue;
//                    if (m.equals(mm)) {
//                        drlModelSet.remove(mm);
//                        break;
//                    }
//                }
//            }
        } catch (Exception e) {
            throw e;
        } finally {
            try {
                if (drlStr != null) drlStr.close();
            } catch (Exception e2) {
                throw e2;
            }
        }
    }
    /**
     * 根据facts 解析收集关联数据
     */
    @Override
    public void collectReferenceData(RuleManage ruleManage, List<Fact> facts, Map<String, RuleReferenceData> ruleRefMap) throws Exception {
    	String projectCode = ruleManage.getProjectCode();
        // drl数据
        Set<String> drlModelSet = new HashSet<String>();
        Set<String> drlModelThenSet = new HashSet<String>();
        
		for(Fact fact:facts) {
			getDrlModelSet(projectCode,fact.getContent(),drlModelSet,drlModelThenSet);
		}
        // 读取模型数据,进行匹配
		collectReferenceData(ruleManage, drlModelSet, drlModelThenSet, ruleRefMap);
    }
    

	@Override
	public void collectReferenceData(RuleManage ruleManage, String funcDrlContent, Map<String, RuleReferenceData> ruleRefMap) throws Exception {
		String projectCode = ruleManage.getProjectCode();
        // drl数据
        Set<String> inRefSet = new HashSet<String>();
        Set<String> outRefSet = new HashSet<String>();
        
        getDrlModelSet(projectCode,funcDrlContent,inRefSet,outRefSet);
        // 读取模型数据,进行匹配
        collectReferenceData(ruleManage, inRefSet, outRefSet, ruleRefMap);
	}        
	
	private void collectReferenceData(RuleManage ruleManage, Set<String> inRefSet,Set<String> outRefSet, Map<String, RuleReferenceData> ruleRefMap) throws Exception {
        Map<String, RuleDataset> map = dataBomService.findRuleDataset(ruleManage.getProjectCode());
        Collection<RuleDataset> list = map.values();
        Map<String, List<String>> getModelMap = new HashMap<String, List<String>>();
        Map<String, List<String>> getThenModelMap = new HashMap<String, List<String>>();
		//输入
        for (String m : inRefSet) {
            if (m.equals("")) continue;
            if (getModelMap.get(m.split("\\.")[0]) != null) {
                getModelMap.get(m.split("\\.")[0]).add(m.split("\\.")[1]);
            } else if (m.split("\\.").length == 2) {
                List<String> getModelAttrList = new ArrayList<String>();
                getModelAttrList.add(m.split("\\.")[1]);
                getModelMap.put(m.split("\\.")[0], getModelAttrList);

            }
        }
        //输出数据
        for (String m : outRefSet) {
            if (m.equals("")) continue;
            if (getThenModelMap.get(m.split("\\.")[0]) != null) {
                getThenModelMap.get(m.split("\\.")[0]).add(m.split("\\.")[1]);
            } else {
                List<String> getModelAttrList = new ArrayList<String>();
                getModelAttrList.add(m.split("\\.")[1]);
                getThenModelMap.put(m.split("\\.")[0], getModelAttrList);
            }
        }
        for (RuleDataset dm : list) {
            for (Entry<String, List<String>> tds : getThenModelMap.entrySet()) {
                if (dm.getName().equals(tds.getKey())) {
                    //List<RuleTestItem> attrList = new ArrayList<RuleTestItem>();
                    RuleItem testItem = null;
                    for (String attr : tds.getValue()) {
                        //attrList.add(as.getAttributeByName(tds.getKey(), attr, path));
                        testItem = new RuleItem();
                        this.getRuleTestItem(testItem, ruleManage.getProjectCode(), dm.getName(), attr);
                        testItem.setClassName(dm.getName());
                        testItem.setClassNamecn(dm.getChzn());
                        String key = testItem.getClassName() + "_" + testItem.getNameen() + "_" + ContentTypeEnum.out.code;
                        testItem.setId(null);
                        testItem.setIsNewRecord(false);
                        this.ruleReferenceDataService.collectCommonData(testItem, key, ContentTypeEnum.out.code, ruleManage, ruleRefMap);
                    }

                }
            }
            for (Entry<String, List<String>> ds : getModelMap.entrySet()) {
                if (dm.getName().equals(ds.getKey())) {
                    //List<RuleTestItem> attrList = new ArrayList();
                    RuleItem testItem = null;
                    for (String attr : ds.getValue()) {
                        //attrList.add(as.getAttributeByName(ds.getKey(), attr, path));
                        testItem = new RuleItem();
                        this.getRuleTestItem(testItem, ruleManage.getProjectCode(), dm.getName(), attr);
                        testItem.setClassName(dm.getName());
                        testItem.setClassNamecn(dm.getChzn());
                        String key = testItem.getClassName() + "_" + testItem.getNameen() + "_" + ContentTypeEnum.in.code;
                        testItem.setId(null);
                        testItem.setIsNewRecord(false);
                        this.ruleReferenceDataService.collectCommonData(testItem, key, ContentTypeEnum.in.code, ruleManage, ruleRefMap);
                    }
                }
            }
        }
	}
    
    @SuppressWarnings("unchecked")
	private void getRuleTestItem(RuleItem ruleTestItem, String projectCode,String className, String nameen) throws Exception {
        Map<String,RuleDataparams> className2RDP = null;
        String cacheKey = DataCache.getCacheKey(projectCode, DataCache.RULEDATAPARAMS_MAP_OPTION);
        if( DataCache.canGetFromCache(cacheKey) ) {
        	className2RDP = (Map<String, RuleDataparams>) DataCache.getValue(cacheKey);
        }else {
        	className2RDP = new HashMap<String,RuleDataparams>();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("projectcode",projectCode);
            List<RuleDataparams> list = this.ruleDataparamsDao.findParmByClassName(map);
            Map<String, List<RuleDataparams>> mapList = new HashMap<String, List<RuleDataparams>>();
            List<RuleDataparams> rdps = null;
            for(RuleDataparams ruleDataparams:list) {
            	String key = ruleDataparams.getClassName() + "." + ruleDataparams.getName();
            	className2RDP.put(key, ruleDataparams);
            	String key2 = ruleDataparams.getDatasetId();
            	if( mapList.containsKey(key2) ) {
            		rdps = mapList.get(key2);
            	}else {
            		rdps = new ArrayList<>();
            		mapList.put(key2, rdps);
            	}
            	rdps.add(ruleDataparams);
            }
            DataCache.setValue(cacheKey, className2RDP);
            DataCache.setValue(DataCache.getCacheKey(projectCode, DataCache.RULEDATAPARAMS_LIST_OPTION), mapList);
        }
    	String key = className + "." + nameen;
    	RuleDataparams rdp = className2RDP.get(key);
    	if(rdp!=null) {
    		BeanUtils.copyProperties(ruleTestItem, rdp);
    		ruleTestItem.setNameen(rdp.getName());
    		ruleTestItem.setNamecn(rdp.getChzn());
    		ruleTestItem.setDataType(rdp.getType());
    	}
        

    }

    /**
     * 通过数据模型ID获取模型信息
     */
    public BomField getAttrById(String projectCode, String attrId) throws Exception {
        List<Bom> list = BomCreator.findTopObject(this.dataBomService.findBomMapByProjectCode(projectCode,null));
        if (list == null || list.size() == 0)
            return null;
        for (Bom item : list) {
            if (item.getBomFields() == null || item.getBomFields().size() == 0)
                continue;
            for (BomField field : item.getBomFields()) {
                field.setClassName(item.getName());
                if (field.getId().equals(attrId)) {
                    return field;
                }
            }
        }
        return null;
    }

    /**
     * 匹配map方法 eg.RuleHelper.setValue($insertMap, "PersonalInfo.name",
     * (String)(((Map)($insertMap.get("PersonalInfo"))).get("name")) +
     * (String)(((Map)($insertMap.get("PersonalInfo"))).get("gender")));
     * ((Map)($insertMap.get("PersonalInfo"))).get("name")
     */
    private static Set<String> matcherMapRuleFile(String drlLineStr) {
        Set<String> matcherSet = new HashSet<String>();
        //Math.min((Double)(((Map)($insertMap.get("CalResult"))).get("loanLimit")),(Double)(((Map)($insertMap.get("CalResult"))).get("incomeLimit"))));
        String reg = ".*?\\({2,}Map\\)\\(\\$insertMap\\.get\\((\\S*)\\){2,}\\.get\\((\\S*)\\){2,}.*?";
        if (drlLineStr.matches(reg)) {
            Pattern pattern = Pattern.compile(reg);
            String[] x = drlLineStr.split(",");
            for (String str : x) {
                Matcher matcher = pattern.matcher(str);
                String group = new String();
                while (matcher.find()) {
                    group = matcher.group(1) + "." + matcher.group(2);
                    group = group.replaceAll("\"|\\)", "");
                    matcherSet.add(group);
                }
            }
        }
        return matcherSet;
    }
    /**
     * 匹配map方法 eg.RuleHelper.setValue($insertMap, "PersonalInfo.name",
     * (String)(((Map)($insertMap.get("PersonalInfo"))).get("name")) +
     * (String)(((Map)($insertMap.get("PersonalInfo"))).get("gender")));
     * ((Map)($insertMap.get("PersonalInfo"))).get("name")
     */
    private static Set<String> matcherMapRuleFileDataMap(String drlLineStr) {
        Set<String> matcherSet = new HashSet<String>();
        //Math.min((Double)(((Map)($insertMap.get("CalResult"))).get("loanLimit")),(Double)(((Map)($insertMap.get("CalResult"))).get("incomeLimit"))));
        String reg = ".*?\\({2,}Map\\)\\(dataMap\\.get\\((\\S*)\\){2,}\\.get\\((\\S*)\\){2,}.*?";
        if (drlLineStr.matches(reg)) {
            Pattern pattern = Pattern.compile(reg);
            String[] x = drlLineStr.split(",");
            for (String str : x) {
                Matcher matcher = pattern.matcher(str);
                String group = new String();
                while (matcher.find()) {
                    group = matcher.group(1) + "." + matcher.group(2);
                    group = group.replaceAll("\"|\\)", "");
                    matcherSet.add(group);
                }
            }
        }
        return matcherSet;
    }
    private static Set<String> matcherOtherRuleFile2(String drlLineStr) {
        Set<String> matcherSet = new HashSet<String>();
        //String reg = ".*?ruleHelper.getValue\\(.*?(\\S*)\\,.*?(\\S*?)\\).*?";
        String reg = ".*?ruleHelper.\\s*getValue\\(\\s*(dataMap|\\$insertMap)\\s*\\,\\s*([A-Za-z0-9_.\\$\"]*)\\s*\\).*?";
        if (drlLineStr.matches(reg)) {
            Pattern pattern = Pattern.compile(reg);
            Matcher matcher = pattern.matcher(drlLineStr);
            String group = new String();
            while (matcher.find()) {
                group = matcher.group(2);
                group = group.replaceAll("\"", "");
                matcherSet.add(group);
            }
        }
        return matcherSet;
    }

    private static Set<String> matcherOtherRuleFile3(String drlLineStr) {
        Set<String> matcherSet = new HashSet<String>();
        String reg = ".*?MathHelper.invokeMathToObj\\(\\s?\\$insertMap\\s?\\,\\s?metaMap\\s?\\,.*?\"(\\S*)\"\\,.*?(\\S*)\\).*?";
        if (drlLineStr.matches(reg)) {
            Pattern pattern = Pattern.compile(reg);
            Matcher matcher = pattern.matcher(drlLineStr);
            String group = new String();
            while (matcher.find()) {
                group = matcher.group(1);
                matcherSet.add(group);
            }
        }
        reg = ".*?(\\{\\w{8}\\}).*?";
        if (drlLineStr.matches(reg)) {
            Pattern pattern = Pattern.compile(reg);
            Matcher matcher = pattern.matcher(drlLineStr);
            String group = new String();
            while (matcher.find()) {
                group = matcher.group(1);
                matcherSet.add(group);
            }
        }
        return matcherSet;
    }

    /**
     * 匹配除了this eg.RuleHelper.setValue($insertMap, "Result.riskLevel", "4")
     */
    private Set<String> matcherOtherRuleFile(String drlLineStr) {
        Set<String> matcherSet = new HashSet<String>();
        String reg = ".*?ruleHelper.\\s*setValue\\(\\s*(dataMap|\\$insertMap)\\s*\\,.?(\\S*)\\s*\\,.*(\\S*)\\).*?";
        //String reg = ".*?ruleHelper.setValue\\(.*?(\\S*)\\,.*?(\\S*)\\,.*?(\\S*)\\).*?";
        if (drlLineStr.matches(reg)) {
            Pattern pattern = Pattern.compile(reg);
            Matcher matcher = pattern.matcher(drlLineStr);
            String group = new String();
            while (matcher.find()) {
                group = matcher.group(2);
                group = group.replaceAll("\"", "");
				if(group.contains(",")) {
					group = group.split(",")[0];
				}
                matcherSet.add(group);
            }
        }
        return matcherSet;
    }
    
    /**
     * 匹配函数 eg.ruleHelper.setValue(dataMap, \"testData.em\",   \"01\"); return func_testFMT(dataMap,metaMap);
     */
    private Set<String> matcherRuleFunctionFile(String drlLineStr) {
        Set<String> matcherSet = new HashSet<String>();
        String reg = ".*?func_(\\S*)\\(dataMap\\,metaMap\\).*?";
        if (drlLineStr.matches(reg)) {
            Pattern pattern = Pattern.compile(reg);
            Matcher matcher = pattern.matcher(drlLineStr);
            String group = null;
            while (matcher.find()) {
                group = matcher.group(1);
                matcherSet.add("func."+group);
            }
        }
        return matcherSet;
    }
    
    /**
     * 匹配除了this eg.RuleHelper.setValue($insertMap, "Result.riskLevel", "4")
     */
    private Set<String> matcherOtherRuleFile7(String drlLineStr) {
        Set<String> matcherSet = new HashSet<String>();
        String reg = ".*?ruleHelper.\\s*setValueType\\(\\s*(dataMap|\\$insertMap)\\s*\\,\\s*(\\[A-Za-z0-9_.\\$\"]*)\\s*\\,\\s*(\\S*)\\s*\\,\\s*(\\S*)\\s*\\).*?";
        //String reg = ".*?ruleHelper.setValue\\(.*?(\\S*)\\,.*?(\\S*)\\,.*?(\\S*)\\).*?";
        if (drlLineStr.matches(reg)) {
            Pattern pattern = Pattern.compile(reg);
            Matcher matcher = pattern.matcher(drlLineStr);
            String group = new String();
            while (matcher.find()) {
                group = matcher.group(2);
                group = group.replaceAll("\"", "");
				if(group.contains(",")) {
					group = group.split(",")[0];
				}
                matcherSet.add(group);
            }
        }
        return matcherSet;
    }
    /**
     * 匹配除了this eg.RuleHelper.setValue($insertMap, "Result.riskLevel", "4")
     */
    private Set<String> matcherOtherRuleFile6(String drlLineStr) {
        Set<String> matcherSet = new HashSet<String>();
        String reg = ".*?ruleHelper.\\s*deAccumulating\\(\\s*(dataMap|\\$insertMap)\\s*\\,\\s*([A-Za-z0-9_.\\$\"]*)\\s*\\,\\s*([A-Za-z0-9_.\\$\"]*)\\s*\\,\\s*(\\S*)\\s*\\,\\s*(\\S*)\\s*\\).*?";
        if (drlLineStr.matches(reg)) {
            Pattern pattern = Pattern.compile(reg);
            Matcher matcher = pattern.matcher(drlLineStr);
            String group = new String();
            while (matcher.find()) {
                group = matcher.group(2);
                group = group.replaceAll("\"", "");
                matcherSet.add(group);
                
                group = matcher.group(3);
                group = group.replaceAll("\"", "");
                matcherSet.add(group);
            }
        }
        return matcherSet;
    }

    /**
     * 匹配除了this eg.RuleHelper.setValue($insertMap, "Result.riskLevel", "4")
     */
    private Set<String> matcherInsertValueRuleFile(String drlLineStr) {
        Set<String> matcherSet = new HashSet<String>();
        String reg = ".*?ruleHelper.insertValue\\(.*?(\\S*)\\,.*?(\\S*)\\,.*?(\\S*)\\).*?";
        if (drlLineStr.matches(reg)) {
            Pattern pattern = Pattern.compile(reg);
            Matcher matcher = pattern.matcher(drlLineStr);
            String group = new String();
            while (matcher.find()) {
                group = matcher.group(2);
                group = group.replaceAll("\"", "");
                matcherSet.add(group);
            }
        }
        return matcherSet;
    }

    /**
     * 匹配this方法 eg.this["PersonalInfo"]["yearIncome"]
     */
    private Set<String> matcherRuleFile(String drlLineStr) {
        Set<String> matcherSet = new HashSet<String>();
        String reg = ".*this\\[.*";
        String regex = "(this(\\[\\S*?(\\]\\[)?+\\])+?)";
        if (drlLineStr.matches(reg)) {
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(drlLineStr);
            String group = new String();
            while (matcher.find()) {
                group = matcher.group(2);
                group = group.replaceAll("(\"\\])(\\[\")", "\\.");
                group = group.replaceAll("(\\[\")|(\"\\])", "");
                matcherSet.add(group);
            }

        }
        return matcherSet;
    }
	private static Set<String> matcherOtherRuleFile5(String drlLineStr) {
		Set<String> matcherSet = new HashSet<String>();
		String reg = ".*?ruleHelper.getValue\\(\\$insertMap\\,?(\\S*)\\){0,}.+?";
		if (drlLineStr.matches(reg)) {
			String[] arr = drlLineStr.split("ruleHelper\\.getValue");
			for(int i=0;i<arr.length;i++) {
				String regS = ".*?\\(\\$insertMap\\,\"?(\\S*)\"\\).*?";				
				Pattern pattern = Pattern.compile(regS);
				Matcher matcher = pattern.matcher(arr[i]);
				String group = "";
				while (matcher.find()) {
					group = matcher.group(1);
					matcherSet.add(group);
				}
			}
			for(int i=0;i<arr.length;i++) {
				String regS = ".*?\\(\\$insertMap\\,\"?(\\S*)\"\\).+?";				
				Pattern pattern = Pattern.compile(regS);
				Matcher matcher = pattern.matcher(arr[i]);
				String group = "";
				while (matcher.find()) {
					group = matcher.group(1);
					matcherSet.add(group);
				}
			}
		}
		return matcherSet; 
	}
	
	/**
	 * 获取解析对象
	 */
	public RuleParseDrl getParseObj(String projectCode) {
        Map<String, Bom> indexBom = dataBomService.findBomMapByProjectCode(projectCode,null);
        Map<String, Bom> handlerBom = BomCreator.findMapInOut(indexBom);
        Map<String, Domain> drlDomain = this.dataBomService.findDomainByProjectCode(projectCode);
        List<JFun> jFuns = ruleCodeblockService.genJFun(projectCode);
        Map<String,String> funcParamMap = ruleDataparamsService.getdataparams(projectCode);
        RuleParseDrl ruleParseDrl = new RuleParseDrl(indexBom, handlerBom, drlDomain, jFuns);
        ruleParseDrl.setFuncParamMap(funcParamMap);
        return ruleParseDrl;
	}
}
